import store from '@/store'
import axios from 'axios'
import { Toast } from 'vant'

const axiosInstance = axios.create({
    baseURL: "http://127.0.0.1:3000/v1",
    timeout: 5000
})

// 请求拦截器
axiosInstance.interceptors.request.use(config => {
    if (store.state.token) {
        config.headers["softeem_token"] = store.state.token
    }
    return config
})

// 响应拦截器
axiosInstance.interceptors.response.use(resp => {
    Toast.clear(true);
    if (resp.data.status == 'success') {
        return Promise.resolve(resp.data.data)
    } else {
        // 逻辑失败
        Toast.fail(resp.data.msg)
        return Promise.resolve(resp.data)
    }
}, error => {
    Toast.clear(true)
    Toast.fail('请求失败')
    return Promise.reject(error)
})

export default axiosInstance;